import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:freedom/entity/bottom_item.dart';
import 'package:freedom/entity/restful.dart';
import 'package:freedom/page/dialog/confirm.dart';
import 'package:freedom/page/game_index.dart';
import 'package:freedom/util/api_router.dart';
import 'package:freedom/util/customer_dialog.dart';
import 'package:freedom/util/http.dart';
import 'package:freedom/util/navigator_provider.dart';
import 'package:freedom/util/socket_server.dart';

class MenuDialogWidget extends StatefulWidget {
  const MenuDialogWidget({super.key});

  @override
  State<MenuDialogWidget> createState() => _MenuDialogWidgetState();
}

class _MenuDialogWidgetState extends State<MenuDialogWidget>
    with SingleTickerProviderStateMixin {
  // 侧拉菜单
  List<BottomItem>? _drawItemList;
  late AnimationController _animationController;

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      duration: const Duration(milliseconds: 200),
      vsync: this,
    );

    _animationController.forward();
    _drawItemList = [
      BottomItem.init(
          icon: Icons.exit_to_app,
          name: '离开房间',
          action: () {
            CustomerDialog.showGamingDialog((closeWindow) {
              return Confirm(
                closeWindow: closeWindow,
                confirm: () {
                  Http.getInstance().request<Restful>(ApiRouter.exitRoom,
                      contentType: ContentType.json,
                      type: RequestEnum.POST,
                      showErrorMsg: true, success: (data) {
                    closeWindow();
                    SocketServer().closeSocket();
                    CustomerDialog.closeAllLoading();
                    NavigatorProvider.pushAndRemoveUntil(const GameIndex(
                      autoAddHome: false,
                    ));
                  });
                },
                cancel: () {
                  closeWindow();
                },
                tips: "确定关闭房间吗",
              );
            });
          }),
      BottomItem.init(
          icon: Icons.exit_to_app,
          name: '申请解散',
          action: () {
            CustomerDialog.showText("暂未开发,请等待");
            /*   CommonDialog.showConfirmDialog(
                confirm: () {
                  NavigatorProvider.pop();
                  CustomerDialog.showGamingDialog(GameDissolution());
                },
                tips: "申请解散吗?"); */
            /*  CustomerDialog.closeAllLoading(
                groupKey: CustomerDialog.GAME_MENU_GROUP); */
            /* Navigator.of(NavigatorProvider.navigatorContext.context)
                .pushAndRemoveUntil(
                    new CustomRoute(
                        GameIndex(
                          autoAddHome: false,
                        ),
                        AnimationEnum.zoom),
                    (route) => false); */
          }),
      BottomItem.init(
          icon: Icons.settings,
          name: '游戏设置',
          action: () {
            CustomerDialog.showText("暂未开发,请等待");
          }),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: const BoxDecoration(
          gradient: LinearGradient(
              begin: Alignment.centerRight,
              end: Alignment.centerLeft,
              stops: [
            0.0,
            1.0
          ], //[渐变起始点, 渐变结束点]
              //渐变颜色[始点颜色, 结束颜色]
              colors: [
            Color.fromARGB(255, 0, 0, 0),
            Color.fromARGB(20, 36, 41, 46)
          ])),
      child: Row(
        children: [
          Expanded(
              child: Material(
            color: Colors.transparent,
            child: InkWell(
                onTap: () {
                  CustomerDialog.closeAllLoading(
                      groupKey: CustomerDialog.GAME_MENU_GROUP);
                },
                child: const SizedBox(
                  width: double.infinity,
                  height: double.infinity,
                )),
          )),
          SizedBox(
            width: 200.w,
            child: ListView(
                children: _drawItemList!
                    .map((ditem) => MaterialButton(
                          onPressed: ditem.action,
                          child: Container(
                              height: 40.h,
                              width: 40.h * 4,
                              decoration: const BoxDecoration(
                                  border: Border(
                                      bottom: BorderSide(
                                          width: 2,
                                          color: Color.fromARGB(
                                              28, 158, 158, 158)))),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Icon(
                                    ditem.icon,
                                    color: Colors.white,
                                    size: 15.h,
                                  ),
                                  SizedBox(
                                    width: 10.h,
                                  ),
                                  Text(
                                    ditem.name!,
                                    style: TextStyle(
                                        color: Colors.white, fontSize: 15.h),
                                  ),
                                ],
                              )),
                        ))
                    .toList()),
          )
        ],
      ),
    );
  }
}
